package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.StandTecRoutProc;
import com.mxpio.erp.common.technology.service.StandTecRoutProcService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author wupaa
 */
@Tag(name = "StandTecRoutProcController", description = "标准工艺路线明细信息接口")
@RestController
@RequestMapping("/erp/tech/standrout/proc/")
public class StandTecRoutProcController {

    @Autowired
    private StandTecRoutProcService service;

    @GetMapping("list")
    @Operation(summary = "标准工艺路线明细列表", description = "获取标准工艺路线明细列表（无分页）", method = "GET")
    public Result<List<StandTecRoutProc>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<StandTecRoutProc> lines = service.list(StandTecRoutProc.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "标准工艺路线明细列表", description = "获取标准工艺路线明细列表（分页）", method = "GET")
    public Result<Page<StandTecRoutProc>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<StandTecRoutProc> lines = service.listPage(StandTecRoutProc.class, page, criteria);
        return Result.OK(lines);
    }

    @GetMapping("list/{routCode}")
    @Operation(summary = "根据routCode获取标准工艺路线明细", description = "根据routCode获取标准工艺路线明细", method = "GET")
    public Result<List<StandTecRoutProc>> listByBizNo(@Parameter(description="业务单号") @PathVariable(name = "routCode", required = true) String routCode) {
        Criteria c = Criteria.create().addCriterion("routCode", Operator.EQ, routCode);
        List<StandTecRoutProc> lines = service.list(c);
        if (lines.size()>0){
            lines.forEach(per -> per.setSaveTransient(false));
        }
        return Result.OK(lines);
    }
}
